﻿#region

using System;
using System.Diagnostics;
using System.IO.IsolatedStorage;

#endregion

namespace Minion.Win.Client
{
    public class AppSettings
    {
        private const string ApiBaseUrlDefault = "http://172.31.130.43";
        private const bool LocationConsentDefault = false;
        private static readonly IsolatedStorageSettings isolatedStore;

        //keys for isolated store
        public static string ApiBaseUrl = "ApiBaseUrl";
        public static string LocationConsent = "LocationConsent";

        /// <summary>
        ///     Constructor that gets the application settings.
        /// </summary>
        static AppSettings()
        {
            try
            {
                isolatedStore = IsolatedStorageSettings.ApplicationSettings;
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e);
            }
        }

        public static string ApiUrlSetting
        {
            get { return GetValueOrDefault(ApiBaseUrl, ApiBaseUrlDefault); }
            set
            {
                AddOrUpdateValue(ApiBaseUrl, value);
                Save();
            }
        }


        public static bool LocationConsentSetting
        {
            get { return GetValueOrDefault(LocationConsent, LocationConsentDefault); }
            set
            {
                AddOrUpdateValue(LocationConsent, value);
                Save();
            }
        }

        /// <summary>
        ///     Update a setting value for our application. If the setting does not
        ///     exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (isolatedStore.Contains(Key))
            {
                // If the value has changed
                if (isolatedStore[Key] != value)
                {
                    // Store the new value
                    isolatedStore[Key] = value;
                    valueChanged = true;
                }
            }
                // Otherwise create the key.
            else
            {
                isolatedStore.Add(Key, value);
                valueChanged = true;
            }

            return valueChanged;
        }


        /// <summary>
        ///     Get the current value of the setting, or if it is not found, set the
        ///     setting to the default setting.
        /// </summary>
        /// <typeparam name="valueType"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
        {
            valueType value;

            // If the key exists, retrieve the value.
            if (isolatedStore.Contains(Key))
            {
                value = (valueType) isolatedStore[Key];
            }
                // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }

            return value;
        }


        /// <summary>
        ///     Save the settings.
        /// </summary>
        public static void Save()
        {
            isolatedStore.Save();
        }
    }
}